package com.sevenday.collections;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import com.google.common.collect.*;
import com.google.common.primitives.Doubles;
import com.google.common.primitives.Ints;

import java.util.*;

/**
 * @author sevenDay
 * @classname ListUtils
 * @description: 描述TODO
 * @date 2017/12/2
 */
public class ListUtils {

    public static void main(String[] args) {
        //创建int的集合元素
        List lists = Lists.newArrayList(1,2,3,4,5);

        List list = Ints.asList(1,2,3,4,5);
        //进行元素的反转
        List reverse = Lists.reverse(lists);
        System.out.println(reverse);

        //元素分组，分为两个集合元素
        List partition = Lists.partition(lists, 3);
        System.out.println(partition);

        List persionList = Lists.newArrayList(
                new Person("Wilma", "Flintstone", 30, "F"),
                new Person("Fred", "Flintstone", 32, "M"),
                new Person("Betty", "Rubble", 31, "F"),
                new Person("Barney", "Rubble", 33, "M"));
        //进行元素的过滤处理
        ImmutableList immutableList = FluentIterable.from(persionList).filter(new Predicate<Person>() {
            @Override
            public boolean apply(Person person) {
                return person.getAge() >= 33;
            }
        }).toList();

        System.out.println(persionList);
        System.out.println(immutableList);

        //将List集合中的对象构造为String的返回参数的List的集合
        ImmutableList immutableList1 = FluentIterable.from(persionList).transform(new Function<Person, String>() {
            @Override
            public String apply(Person input) {
                return Joiner.on('#').join(input.getName(),
                        input.getPosition(), input.getAge(),input.getSex());
            }
        }).toList();
        System.out.println(immutableList1);

        Set<Integer> set1 = Sets.newHashSet(new Integer(1),new Integer(2),new Integer(3),new Integer(4),new Integer(5));

        Set<Integer> set2 = Sets.newHashSet(new Integer(2),new Integer(3),new Integer(4),new Integer(5),new Integer(6));

        //进行set中两个不同元素的获取[获取set1中有的集合，set2中没有的集合]
        final Sets.SetView<Integer> difference = Sets.difference(set1, set2);
        final HashSet<Integer> integers = Sets.newHashSet(difference);
        System.out.println("integers = " + integers);

        //返回两个集合的补集元素
        final Sets.SetView<Integer> integers1 = Sets.symmetricDifference(set1, set2);
        System.out.println("integers1 = " + integers1);
        
        //返回集合的交集元素
        final Sets.SetView<Integer> intersection = Sets.intersection(set1, set2);
        System.out.println("intersection = " + intersection);

        //返回元素的并集
        final Sets.SetView<Integer> union = Sets.union(set1, set2);
        System.out.println(union);

        //将List转化为Map的对象,返回的key不能有相同的key否则会出错
        Map<String,Person> map= Maps.uniqueIndex(persionList.iterator(), new Function<Person, String>() {
            @Override
            public String apply(Person input) {

                return input.getPosition();
            }
        });
        System.out.println("map = " + map);


    }


}
